package io.github.yotta.common.server.example.controller;

import io.github.yotta.common.api.exception.ServiceException;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.UUID;

/**
 * @author yotta
 * @date 2023/3/8 9:10
 */
@RestController
public class HelloWorldController {

    @GetMapping("api/success")
    public User testNoException() {
        final User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setName("Yotta");
        return user;
    }

    @GetMapping("api/error")
    public User testThrowException() {
        final User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setName("Yotta");
        // 模拟抛出异常
        throw new ServiceException("user.not.find");
    }

    /**
     * 模拟方法不支持这种内置异常
     * @return
     */
    @PostMapping("api/405")
    public User test405() {
        final User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setName("Yotta");
        return user;
    }
}
@Data
class User implements Serializable {
    private String id;

    private String name;

}